package my2Cents.tools;

import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;

import my2Cents.Sandbox;
import my2Cents.Undo;
import my2Cents.Util;
import my2Cents.properties.CheckBox;
import my2Cents.properties.Property;
import my2Cents.properties.Slider;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.BasicJoint;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PBox;
import net.phys2d.raw.shapes.PLine;

public class LineTool extends AbstractBodyMaker {
	protected Vector2f startPos;
	protected Vector2f endPos;
	private int width = 1;
	private boolean autoAttach = false;
	
	public LineTool() {
		setName("Line");
		setDescription(
				"Click and drag to create a Line."
			);
		Property w = new Slider("Width",0,0,30,1,false) {
			private static final long serialVersionUID = 1L;

			public void valueChanged(int value) {
				width = value;
			}
		};
		Property chk = new CheckBox("Create Hinged", false) {
			private static final long serialVersionUID = 1L;

			public void valueChanged(boolean v) {
				autoAttach = v;
			}
		};
		this.addProperty(chk);
		this.addProperty(w);
	}
	
	public void startTool(MouseEvent arg0) {
		super.startTool(arg0);
		startPos = new Vector2f(arg0.getX(), arg0.getY());
		endPos = new Vector2f(arg0.getX(), arg0.getY());
	}

	public GeneralPath getGhostPath() {
		GeneralPath gp = new GeneralPath();
		gp.moveTo(startPos.x,startPos.y);
		gp.lineTo(endPos.x,endPos.y);
		return gp;
	}
	
	public void toolThink(MouseEvent arg0) {
		endPos = new Vector2f(arg0.getX(), arg0.getY());
		shape = new PLine(startPos, endPos);
	}
	
	public void endTool(MouseEvent arg0) {
		if(startPos.distance(endPos) < 4) {
			super.endTool(arg0);
			return;
		}
		
		float x1 = startPos.x;
		float y1 = startPos.y;
		
		Vector2f finalStart = startPos.copy();
		Vector2f finalEnd = endPos.copy();
		
		double angle = Util.angleVec(startPos,endPos);
		float distance = startPos.distance(endPos);
		float cx = (float) (Math.cos(angle)*distance/2);
		float cy = (float) (Math.sin(angle)*distance/2);
		
		startPos.x = -cx;
		startPos.y = -cy;
		endPos.x = cx;
		endPos.y = cy;
		
		shape = new PBox(distance,width);
		Body b = createBody(5);
		b.setRotation((float) angle);
		b.setPosition(x1 + cx, y1 + cy);
		sandbox.postBodyCreated(b);
		sandbox.onToolEnd();
		
//		Undo lundo = sandbox.getUndoList().getUndo(b, shape.getName());
//		
		if(autoAttach) {
			BasicJoint h1 = Util.setupHinge(finalStart, finalStart, false, true, false, false);
			BasicJoint h2 = Util.setupHinge(finalEnd, finalEnd, false, true, false, false);
			
			//System.out.println("Hinged!");
			
			if(h1 != null) {
				//System.out.println("Got Hinge 1");
				//lundo.groupUndo(new Undo(h1,getWorld(),"ConstraintA"));
			}
			
			if(h2 != null) {
				//System.out.println("Got Hinge 2");
				//lundo.groupUndo(new Undo(h2,getWorld(),"ConstraintB"));
			}
		}
		
		super.endTool(arg0);
	}

}
